﻿#!/usr/bin/python
import sys		# For command line args
import os		# For file manipulations

class Settings:
	ScriptName = "##SCRIPTNAME##" 		# Will be assigned
#	FolderSeparator = "/"				# Depends on the system

class Data:
	"""stores the actual data with file names and file contents"""
	fileList= list();
	def Install(self):
		print "\nInstallation Mode."
		ValidateFileCreationPermissions(self)
		CreateFiles(self)
		print "Installation Complete."
		print "To setup dataset parameters please execute the following macro from the Topspin."
		print self.fileList[0].FullFileName
	def Clear(self):
		print "\nCleaning Mode."
		ValidateFileDeletionPermissions(self)
		DeleteFiles(self)
	def Propagate(self):
		print "Propagation Mode."
		print "Propagation Mode is not implemented yet."
	def __init__(self):
		# Filling in data
#__FILELIST__#
		#self.fileList.append(File("topspin.mac", "/home/Vadim/TopSpinTest/macros/", "MacroFile\nSecondLine"))
		#self.fileList.append(File("pulseprogram.pk", "/home/Vadim/TopSpinTest/pp/", "FirstContent\nSecond Line"))
		#self.fileList.append(File("cpd.gy", "/home/Vadim/TopSpinTest/cpd/", "SecondContent\nSecondLine"))
		# End of data filling in
		print "\nRealData class instance is initialized successfully."
		
# Main Entry Point
def main():
	print "Setup Experiment Python Script. Welcome!"
	if len(sys.argv)==1 or len(sys.argv)>2:
		ShowSimpleHelp()
		return
	option = sys.argv[1]
	# Command Line Options
	RealData = Data()
	if option == "-s":
		RealData.Install()
	elif option == "-c":
		RealData.Clear()
	elif option == "-p":
		RealData.Propagate()
	else:
		print "Wrong Option."
		ShowSimpleHelp()
		return
	return
	
# Misc Functions
class File:
	"""stores single file entry"""
	FullFileName=''
	FilePath=''
	Content=''
	def __init__(self, fileName, path, content):
		self.FullFileName= path+fileName
		self.FilePath = path
		self.Content=content
		return

def CreateFiles(obj):
	print "Writing files."
	for file in obj.fileList:
		print file.FullFileName,
		fout = open(file.FullFileName, "w")
		fout.write(file.Content)
		fout.close()
		print " OK"
	
def ValidateFileCreationPermissions(obj):
	print "Validating file creation permissions." 
	errors = list()
	for file in obj.fileList:
		if not os.path.exists(file.FilePath):
			try:
				os.mkdir(file.FilePath)
			except OSError:	
				errors.append("ERROR: Cannot create directory " + file.FilePath)
		elif not os.access(file.FilePath, os.W_OK):
			errors.append("Writing to " + file.FilePath+" is not permitted.")
		if os.path.exists(file.FullFileName):
			errors.append("File with the name "+file.FullFileName+" already exists.")
	if len(errors)!=0:
		print "There are problems with file copying."
		for error in errors:
			print "ERROR: ", error
		print "No Files were copied."
		exit()
	else:
		print "Required file creation permissions are granted."
	return

def DeleteFiles(obj):
	print "Deleting Setup Files"
	for file in obj.fileList:
		print file.FullFileName,
		if not os.path.isfile(file.FullFileName):
			print "does not exists."
			continue
		try:	
			os.remove(file.FullFileName)
			print " DELETED"
		except OSError:
			print "\nERROR: Cannot delete " + file.FullFileName+"."
	
def ValidateFileDeletionPermissions(obj):
	pass

def ShowSimpleHelp():
	print "Usage: "+Settings.ScriptName + " <option>"
	print "\t"+Settings.ScriptName+" -s: to setup the experiment."
	print "\t"+Settings.ScriptName+" -c: to clean the crap from previously setuped experiment."
	print "\t"+Settings.ScriptName+" -p: to propagate experiment setup into the system."
	return
	
if __name__ == '__main__':
	main()
